package com.wgts.wgts.mvp.view.activity.mycenter;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAddAddressActivityComponent;
import com.wgts.wgts.dagger.module.AddAddressActivityModule;
import com.wgts.wgts.mvp.presenter.IAddAddressActivityPresenter;
import com.wgts.wgts.mvp.view.IAddAddressActivityView;
import com.wgts.wgts.netmodle.resp.ShippingAddressEntity;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.citywheel.CityParseHelper;
import com.lljjcoder.style.citypickerview.CityPickerView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2815:43
 *
 */public class AddAddressActivity extends BaseActivity implements IAddAddressActivityView{
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.address_ly)
    RelativeLayout addressLy;
    @BindView(R.id.save_address)
    RelativeLayout saveAddress;
    @BindView(R.id.detail_address)
    EditText detailAddress;

    @Inject
    IAddAddressActivityPresenter presenter;

    private String sProvince;
    private String sCity;
    private String sArea;
    private static final int AddAddressResult = 0;

    public static final String ADDRES_INFO = "ADDRES_INFO";

    private ShippingAddressEntity shippingAddressEntity;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_center_add_address_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        if(shippingAddressEntity == null){
            includeTitle.setText("添加地址");
        }else{
            includeTitle.setText("修改地址");
            name.setText(shippingAddressEntity.userName);
            phone.setText(shippingAddressEntity.userPhone);
            address.setText(shippingAddressEntity.province + shippingAddressEntity.city + shippingAddressEntity.area);
            sProvince = shippingAddressEntity.province;
            sCity = shippingAddressEntity.city;
            sArea = shippingAddressEntity.area;
            detailAddress.setText(shippingAddressEntity.details);
        }
    }

    @Override
    public void init() {
        DaggerAddAddressActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .addAddressActivityModule(new AddAddressActivityModule(this))
                .build()
                .inject(this);

        shippingAddressEntity = getIntent().getParcelableExtra(ADDRES_INFO);
    }

    @OnClick({R.id.back_btn_ly, R.id.address_ly, R.id.save_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                finish();
                break;
            case R.id.address_ly:
                textDismiss(addressLy);
                selectAddress();
                break;
            case R.id.save_address:
                addAddress();
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {
        dialogDismiss();
        MoHuToast.showToast(msg);
    }

    @Override
    public void addAddressResult() {
        dialogDismiss();
        Intent intent = new Intent();
        intent.putExtra("RESULT_OK", "SUCCESS");
        setResult(AddAddressResult, intent);
        finish();
    }

    private void selectAddress() {
        CityPickerView cityPickerView = new CityPickerView();
        cityPickerView.init(this);
        CityConfig cityConfig = new CityConfig.Builder()
                .title("地址选择")
                .province("安徽省")
                .city("合肥市")
                .district("高新区")
                .setLineColor("#00FFFFFF")
                .build();
        cityPickerView.setConfig(cityConfig);
        cityPickerView.showCityPicker();
        cityPickerView.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                super.onSelected(province, city, district);
                sProvince = province.getName();
                sCity = city.getName();
                sArea = district.getName();
                address.setText(sProvince + " " + sCity + " " + sArea);
            }

            @Override
            public void onCancel() {
                super.onCancel();
            }
        });
    }

    private void addAddress(){
        if (name.getText().toString().trim() == null || name.getText().toString().trim().equals("")){
            MoHuToast.showToast("请输入收货人姓名");
            return;
        }
        if(phone.getText().toString().trim().length() < 11){
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        if (phone.getText().toString().trim() == null || phone.getText().toString().trim().equals("")){
            MoHuToast.showToast("请输入正确的手机号");
            return;
        }
        if (sProvince == null || sProvince.equals("")){
            MoHuToast.showToast("请选择所在地");
            return;
        }
        if (detailAddress.getText().toString().trim() == null || detailAddress.getText().toString().trim().equals("")){
            MoHuToast.showToast("请输入详细收货地址");
            return;
        }
        if(shippingAddressEntity == null){
            showDialog();
            presenter.addAddressResult("", name.getText().toString().trim(), phone.getText().toString().trim(),
                    sProvince, sCity, sArea, detailAddress.getText().toString().trim(), "0");
        }else{
            showDialog();
            presenter.addAddressResult(shippingAddressEntity.addressId, name.getText().toString().trim(), phone.getText().toString().trim(),
                    sProvince, sCity, sArea, detailAddress.getText().toString().trim(), "0");
        }
    }
}
